package com.ipangyang.aidldemo;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.ipangyang.aidldemo.ipclient.Book;
import com.ipangyang.aidldemo.ipclient.BookManager;

import java.util.Random;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private static final String TAG = MainActivity.class.getName();
    boolean connected = false;
    BookManager bookManager = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.intag).setOnClickListener(this);
        findViewById(R.id.outtag).setOnClickListener(this);
        findViewById(R.id.inouttag).setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        attemptBindService();
    }

    @Override
    protected void onStop() {
        super.onStop();
        unbindService(conn);
    }

    @Override
    public void onClick(View v) {
        if(!connected){
            attemptBindService();
            return;
        }

        switch (v.getId()){
            case R.id.intag:
                addBookIn("In");
                break;
            case R.id.outtag:
                addBookOut("Out");
                break;
            case R.id.inouttag:
                addBookIO("In-Out");
                break;
        }

        printLatestList();

    }

    void addBookIn(String tag){
        Book book = new Book();
        book.setName("XXX从入门到放弃之"+tag);
        book.setPrice(new Random(11).nextInt(50));
        try {
            Log.d(TAG, "--->before addBook: " + book);
            Book book2 = bookManager.addBookIn(book);
            Log.d(TAG, "--->after addBook: " + book2);
        } catch (RemoteException e) {
            Log.e(TAG, "addBook: ", e);
        }
    }

    void addBookOut(String tag){
        Book book = new Book();
        book.setName("XXX从入门到放弃之"+tag);
        book.setPrice(new Random(13).nextInt(60));
        try {
            Log.d(TAG, "--->before addBook: " + book);
            Book book2 = bookManager.addBookOut(book);
            Log.d(TAG, "--->after addBook: " + book2);
        } catch (RemoteException e) {
            Log.e(TAG, "addBook: ", e);
        }
    }

    void addBookIO(String tag){
        Book book = new Book();
        book.setName("XXX从入门到放弃之"+tag);
        book.setPrice(new Random(17).nextInt(70));
        try {
            Log.d(TAG, "--->before addBook: " + book);
            Book book2 = bookManager.addBookInout(book);
            Log.d(TAG, "--->after addBook: " + book2);
        } catch (RemoteException e) {
            Log.e(TAG, "addBook: ", e);
        }
    }

    void printLatestList(){
        try {
            Log.i(TAG, "printLatestList: "+bookManager.listAll());
        } catch (RemoteException e) {
            Log.e(TAG, "printLatestList: ", e);
        }
    }


    void attemptBindService(){
        Log.d(TAG, "attemptBindService: ");
        Intent intent = new Intent();
        intent.setAction("com.ipangyang.aidldemo.ipctest");
        intent.setPackage("com.ipangyang.aidldemo");
        bindService(intent,conn,BIND_AUTO_CREATE);
    }

    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d(TAG, "onServiceConnected: ----------------->");
            bookManager = BookManager.Stub.asInterface(service);
            connected = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d(TAG, "onServiceDisconnected: ");
            connected = false;
        }
    };


}
